package cn.iocoder.yudao.module.hs.controller.admin.rawmaterialorder;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.hs.controller.admin.rawmaterialorder.vo.*;
import cn.iocoder.yudao.module.hs.dal.dataobject.rawmaterialorder.RawMaterialOrderDO;
import cn.iocoder.yudao.module.hs.service.rawmaterialorder.RawMaterialOrderService;

@Tag(name = "管理后台 - 领料单信息")
@RestController
@RequestMapping("/hs/raw-material-order")
@Validated
public class RawMaterialOrderController {

    @Resource
    private RawMaterialOrderService rawMaterialOrderService;

    @PostMapping("/create")
    @Operation(summary = "创建领料单信息")
    @PreAuthorize("@ss.hasPermission('hs:raw-material-order:create')")
    public CommonResult<Long> createRawMaterialOrder(@Valid @RequestBody RawMaterialOrderSaveReqVO createReqVO) {
        return success(rawMaterialOrderService.createRawMaterialOrder(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新领料单信息")
    @PreAuthorize("@ss.hasPermission('hs:raw-material-order:update')")
    public CommonResult<Boolean> updateRawMaterialOrder(@Valid @RequestBody RawMaterialOrderSaveReqVO updateReqVO) {
        rawMaterialOrderService.updateRawMaterialOrder(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除领料单信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('hs:raw-material-order:delete')")
    public CommonResult<Boolean> deleteRawMaterialOrder(@RequestParam("id") Long id) {
        rawMaterialOrderService.deleteRawMaterialOrder(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得领料单信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('hs:raw-material-order:query')")
    public CommonResult<RawMaterialOrderRespVO> getRawMaterialOrder(@RequestParam("id") Long id) {
        RawMaterialOrderDO rawMaterialOrder = rawMaterialOrderService.getRawMaterialOrder(id);
        return success(BeanUtils.toBean(rawMaterialOrder, RawMaterialOrderRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得领料单信息分页")
    @PreAuthorize("@ss.hasPermission('hs:raw-material-order:query')")
    public CommonResult<PageResult<RawMaterialOrderRespVO>> getRawMaterialOrderPage(@Valid RawMaterialOrderPageReqVO pageReqVO) {
        PageResult<RawMaterialOrderDO> pageResult = rawMaterialOrderService.getRawMaterialOrderPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RawMaterialOrderRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出领料单信息 Excel")
    @PreAuthorize("@ss.hasPermission('hs:raw-material-order:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRawMaterialOrderExcel(@Valid RawMaterialOrderPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RawMaterialOrderDO> list = rawMaterialOrderService.getRawMaterialOrderPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "领料单信息.xls", "数据", RawMaterialOrderRespVO.class,
                        BeanUtils.toBean(list, RawMaterialOrderRespVO.class));
    }

}